// pages/shopcart/address/address.js
const app = getApp()
var help = require('../../../utils/help.js');
var login = require('../../../utils/login.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    data:[],
    name:'',
    mobile:'',
    address:'',
    id:0,
    addresslists:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id )
    this.setData({  
      id: options.id  
     }) 
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadBaseData()
  },

  loadBaseData: function () {
    var that = this;
    //幻灯片
    help.get(app.data.apiUrl + 'User/address', {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      id:that.data.id
    }, that, function (res, that) {
      var result = res.data;
      console.log(result);

      if (result.code == 1) {
        that.setData({
          data: result.data,
          id: result.data.length > 0 ? result.data[0].id : 0,
          name: result.data.length > 0 ? result.data[0].name : '',
          mobile: result.data.length > 0 ? result.data[0].mobile : '',
          address: result.data.length > 0 ? result.data[0].address : '',
          region: result.data.length > 0 ? [result.data[0].province_str,result.data[0].city_str,result.data[0].area_str] : ['','','']
        })
      }

    });
  },
  set_name: function (e) {
    this.setData({
      name: e.detail.value
    })
    this.loadData(true);
  },

  setaddressid: function (e) {
    var that = this;
    this.setData({
      name: e.detail.value
    })
    this.loadData(true);
  },
  set_mobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
    this.loadData(true);
  },
  set_address: function (e) {
    this.setData({
      address: e.detail.value
    })
    this.loadData(true);
  },

  /**
   * 提交地址
   */
  subAddress: function () {
    var that = this;
    var url = app.data.apiUrl + 'User/address_save';
    var province_name = that.data.region[0];
    var city_name = that.data.region[1];
    var area_name = that.data.region[2];
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      name: that.data.name,
      mobile: that.data.mobile,
      province_name: province_name,
      city_name: city_name,
      area_name: area_name,
      address: that.data.address,
    };
    if (that.data.id > 0) {
      data.id = that.data.id;
    }
    help.get(url, data, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        help.msg('操作成功！');
        setTimeout(function(){
          that.closeAdd();
        }, 500)
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },
  closeAdd: function(e) {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})